package com.nayan.epic;

import java.util.Calendar;
import java.util.Date;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class EpicBroadcastReceiver extends AppWidgetProvider {
	
	private PendingIntent service;
	private final int MINUTES = 4;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		
		Log.i("EPIC", "Widget Created");
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.SECOND, 10);
		
		//final Intent I = ;	  
		
		if(service==null) {
			service = PendingIntent.getService(context, 0, new Intent(context, EpicUpdaterService.class), PendingIntent.FLAG_CANCEL_CURRENT);
		}
		
		alarmManager.setRepeating(AlarmManager.RTC, cal.getTime().getTime(), MINUTES*60*1000, service);
	}

	

	@Override
	public void onDisabled(Context context) {
		Log.i("EPIC", "Updater Service Disabled");
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.cancel(service);
		super.onDisabled(context);
	}
	

}
